#include <QPainter>
#include <QPen>
#include <QRect>
#include <QCursor>
#include <QApplication>
#include <QPixmap>
#include <QImage>
#include <QDebug>

#include "../include/ui_core/mylabel.hpp"

MyQLabel::MyQLabel(QWidget *qw):QLabel(qw)
{
    x1 = 0;
    x2 = 0;
    y1 = 0;
    y2 = 0;
}
void MyQLabel::clear_points( void )
{
    x1 = 0;
    x2 = 0;
    y1 = 0;
    y2 = 0;
}

void MyQLabel::paintEvent(QPaintEvent *event)
{
    //comment before
    QLabel::paintEvent(event);

    QPainter painter(this);

    painter.setPen(QPen(Qt::red, 2));

    if(x1 != x2 || y1 != y2 )
    {
        painter.drawRect(QRect(x1, y1, x2 - x1, y2 - y1));
    }
}

void MyQLabel::mousePressEvent(QMouseEvent *event)
{
    x1 = event->pos().x();
    y1 = event->pos().y();
    x2 = event->pos().x();
    y2 = event->pos().y();
    QCursor cursor;
    cursor.setShape(Qt::ClosedHandCursor);
    QApplication::setOverrideCursor(cursor);
}

void MyQLabel::mouseReleaseEvent(QMouseEvent *event)
{
    x2 = event->pos().x(); //鼠标相对于所在控件的位置
    y2 = event->pos().y();

    update();

    QApplication::restoreOverrideCursor();
}

void MyQLabel::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        x2 = event->pos().x(); //鼠标相对于所在控件的位置
        y2 = event->pos().y();
        update();
    }
}
